package com.yunhe.authority.repository.office;

import com.yunhe.authority.domain.authority.RealStation;
import com.yunhe.authority.domain.office.Office;
import com.yunhe.authority.model.housing.FilterCondition;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 厂房DAO
 * @author zhengbo
 * @date 2020/11/4 16:25
 */
@Repository
public interface OfficeRepository extends Neo4jRepository<Office, Long> {

    /**
     * 厂房绑定企业
     * @param companyId 企业ID
     * @param id 厂房ID
     */
    @Query("match (c:Company),(o:Office) where ID(c)={companyId} and ID(o)={id} create (c)-[r:HAS_OFFICE]->(o)")
    void bindCompanyById(@Param("companyId") Long companyId, @Param("id") Long id);

    /**
     * 厂房解绑企业
     * @param companyId 企业ID
     */
    @Query("match (c:Company)-[r:HAS_OFFICE]->(o:Office) where id(c)={companyId} delete r")
    void unbindCompanyById(@Param("companyId") Long companyId);

    /**
     * 根据企业ID查询厂房列表
     * @param companyId 企业ID
     * @return  Iterable<Office>
     */
    @Query("start c=node({companyId}) match(c)-[r:HAS_OFFICE]->(o) return o")
    Iterable<Office> findByCompanyId(@Param("companyId") Long companyId);

    @Query("start s=node({stationId}) match(s)-[r:HAS_OFFICES{realStationId:{stationId}}]->(o:Office{officeName:{officeName}}) return o")
    Office findByStationIdAndOfficeName(@Param("stationId") Long stationId,@Param("officeName")  String officeName);

    @Query("start s=node({stationId}) match(s)-[r:HAS_OFFICES{realStationId:{stationId}}]->(o:Office{officeTitle:{officeTitle}}) return o")
    Office findByStationIdAndOfficeTitle(@Param("stationId") Long stationId,@Param("officeTitle")  String officeTitle);

    /**
     * 根据总园区id与厂房title查询厂房
     * @param stationId 总园区ID
     * @param officeTitle 厂房title
     */
    @Query("match(o:Office{officeTitle:{officeTitle}})<-[r:HAS_OFFICES]-(rt:RealStation)-[bts:BELONG_TO_STATION]->(s:Station) where id(s)={stationId} return o ")
    Office findOfficeByStationIdAndTitle(@Param("stationId") Long stationId, @Param("officeTitle") String officeTitle);

    @Query("start s=node({stationId}),o=node({id}) create (s)-[r:HAS_OFFICE{stationId:{stationId}}]->(o)")
    void bindStation(@Param("stationId") Long stationId,@Param("id")  Long id);

    /**
     * 根据真实园区id查询厂房列表
     * @param id 真实园区ID
     */
    @Query("match(rt:RealStation)-[r:HAS_OFFICES{realStationId:{id}}]->(o:Office) return o")
    Iterable<Office> findOfficesByRealStationId(@Param("id") Long id);

    @Query("match(rt:RealStation)-[r:HAS_OFFICES]->(o:Office) where ID(rt) IN {realStationIds} return o")
    List<Office> getOfficeByRealStationIds(@Param("realStationIds") List<Long> realStationIds);


    /**
     * 根据企业id和房源id查询房源
     * @param companyId 企业id
     * @param officeId 房源id
     * @return Office
     */
    @Query("match(c:Company)-[r:HAS_OFFICE]->(o:Office) where id(c)={companyId} and id(o)={officeId} return o")
    Office findByCompanyIdAndOfficeId(@Param("companyId") Long companyId,@Param("officeId")  Long officeId);

    /**
     * 查询此厂房是否已经绑定过企业了
     * @param officeId 厂房id
     * @return Office
     */
    @Query("match(c:Company)-[r:HAS_OFFICE]->(o:Office) where id(o)={officeId} return o")
    Office findAlreadyBindCompanyOfficeById(@Param("officeId")  Long officeId);

    /**
     * 根据真实园区id查询空置厂房列表
     * @param id 真实园区ID
     */
    @Query("match(rt:RealStation)-[r:HAS_OFFICES{realStationId:{id}}]->(o:Office) where o.officeStatus='01' return o")
    Iterable<Office> searchVacantOfficesByRealStationId(@Param("id") Long id);

    @Query("match(rt:RealStation)-[r:HAS_OFFICES]->(o:Office) WHERE 1=1   AND ID(rt) ={realStationId} AND o.officeTitle ={officeTitle} RETURN o")
    Office findOfficesByRealStationIdAnOfiiceTitle(@Param("officeTitle")  String officeTitle ,@Param("realStationId")  Long realStationId);
}
